#ifdef _WIN32 
#include <windows.h> 
#endif

#include "Hunter.h"

Hunter::Hunter(glm::vec3 _Pos, float _r, float _speed, std::string _Name,
		std::string _modelFName, ModelPool *_mPool, PathFinder *_pathfinder,
		Terrain::Terrain *_ter) :
		Villager(_Pos, _r, _speed, _Name, _modelFName, _mPool, _pathfinder, _ter)
{

	type = OBJ_HUNTER;
	parentType = OBJ_VILLAGER;

	for (int i = 0; i < 33; i++)
	{
		std::stringstream ss; //create a stringstream
		ss << i + 1; //add number to the stream
		modelID.push_back(mPool->loadModel(modelFName, "_a_00000" + ss.str()));
	}
	deer = NULL;
	costGold = 100;
	dTree = NULL;
	srand (time(NULL));}

Hunter::~Hunter(void)
{
}

void Hunter::AIFunction()
{

	if (dTree == NULL)
		buildDecisionTree();

	if (timer.GetInterval(10000))
	{
		unsigned i = dTree->getDecision();

		if (i == 3)
		{

		}
		else if (i == 5)
		{
			void *ret = pathfinder->getNearestEntityOfType(get2dPosition(), 20,
					OBJ_DEER, STATUS_READY, false);
			deer = static_cast<Deer*>(ret);
			speed = 0.1;
		}
		else if (i == 6)
		{
			goTo(glm::vec2(rand() % 100 + 10, rand() % 100 + 10));
		}
		else if (i == 4)
		{
			goTo(owner->get2dPosition());
			deer = NULL;
		}
	}

	if (timer2.GetInterval(1000) && deer != NULL)
		goTo(deer->get2dPosition());

}

void Hunter::buildDecisionTree()
{
	//Testing function for building simple decision tree.
	// This tree is staticaly defined on begining. This should be moved into
	// scripted part ideally.
	dTree = new DecisionTree(NULL, this, false, 0); //Got target?

	dTree->setYes(new DecisionTree(dTree, this, false, 1)); // Can catch it?
	dTree->getYes()->setYes(new DecisionTree(dTree->getYes(), this, true, 3)); // Continue with action
	dTree->getYes()->setNo(new DecisionTree(dTree->getYes(), this, true, 4)); // No, took so long. Return home

	dTree->setNo(new DecisionTree(dTree, this, false, 2)); // Is valid target nearby?
	dTree->getNo()->setYes(new DecisionTree(dTree->getNo(), this, true, 5)); // yes, set it as target
	dTree->getNo()->setNo(new DecisionTree(dTree->getNo(), this, true, 6)); // no, wander somewhere around a look for a one

}

unsigned int Hunter::resolveDecision(unsigned int id)
{

	if (id == 0)
	{
		//gCon::con->print("HUNTER: Hmm. Do i have a target?");
		if (deer == NULL)
		{
			//gCon::con->print("HUNTER: Hmm. I do not!");
			return 0;
		}
		else
		{
			//gCon::con->print("HUNTER: Actually, i do have a target.");
			return 1;
		}
	}
	else if (id == 1)
	{
		//gCon::con->print("HUNTER: Can i catch it?", id);
		//if unit is too far from it's owning building, send her home.
		int rn = max(abs(owner->get2dPosition().x - get2dPosition().x), abs(owner->get2dPosition().y - get2dPosition().y));
		if (rn > 20)
		{
			//gCon::con->print("HUNTER: Hmm. I can't. Let's go home");
			return 0;
		}
		else
		{
			//gCon::con->print("HUNTER: Sure!");
			return 1;
		}
	}
	else if (id == 2)
	{
		//gCon::con->print("HUNTER: Is some deer around?");

		if (pathfinder->getNearestEntityOfType(get2dPosition(), 10, OBJ_DEER)
				!= NULL)
		{
			//gCon::con->print("HUNTER: It is! Kill it!");
			return 1;
		}
		else
		{
			//gCon::con->print("HUNTER: Hmm. No. Let's wander");
			return 0;
		}

	}
	else
		return 666;
}
